<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <title>添加群成员</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <link rel="stylesheet" href="https://res.wx.qq.com/open/libs/weui/1.1.2/weui.min.css"/>
    <link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.0/css/jquery-weui.css"/>
    <style type="text/css">
        body, html {
            height: 100%;
            background-color: #f4f4f4;
            -webkit-tap-highlight-color: transparent;
        }

        .basic-color {
            background-color: #1E88E5;
            color: #FFFFFF;
        }

        footer {
            text-align: center;
            font-size: 14px;
            padding: 20px;
        }

        .weui-loadmore_line .weui-loadmore__tips {
            background-color: #f4f4f4;
        }

        footer a {
            color: #999;
            text-decoration: none;
        }

    </style>
</head>

<body ontouchstart>
<div class="weui-cells__title weui-cell" style="margin: auto;">
    <div class="weui-cell__hd"><img src=""></div>
    <div class="weui-cell__bd">
        <p></p>
    </div>
    <div class="weui-cell__ft">
        <a class="weui-btn weui-btn-basic weui-btn_mini weui-btn_primary add-user-to-group basic-color"
           style="background-color: #1E88E5;" th:attr="group-id=${siteGroupId}" url-data="/group/addGroupMember"
           href="javascript:">添加入群</a>
    </div>
</div>

<div class="weui-cells weui-cells_checkbox" id='user-lists'>
</div>


<div class="weui-loadmore weui-loadmore_line loadmore_site_user">
    <span class="weui-loadmore__tips"></span>
</div>

<div id="groupID" th:attr="groupId=${siteGroupId}"></div>
<input type='hidden' id='init_url' url-data="/group/nonGroupMember"/>
<input type="hidden" id="add_group_user" site-user-id="">
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/2.2.4/jquery.js"></script>
<script type="text/javascript" src="https://res.wx.qq.com/open/libs/weuijs/1.1.3/weui.min.js"></script>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery-weui/1.2.0/js/jquery-weui.js"></script>
<script type="text/javascript">

    $(function () {
        nonList();
    });

    function getOsType() {
        var u = navigator.userAgent;
        if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {
            return 'Android';
        } else if (u.indexOf('iPhone') > -1) {
            return 'IOS';
        } else {
            return 'PC';
        }
    }

    function reqUrl(reqUri, params, callbackName) {
        var type = getOsType();
        if (type == 'Android') {
            Android.requestPost(reqUri, params, callbackName);
        } else if (type == 'IOS') {
            ios_requestPost(reqUri, params, callbackName);
        } else {
            $.toast('暂时不支持该设备');
        }
    }

    function reqHtml(reqUri, params) {
        var type = getOsType();
        if (type == 'Android') {
            Android.requestPage(reqUri, params)
        } else if (type == 'IOS') {
            ios_requestPage(reqUri, params);
        } else {
            $.toast('暂时不支持该设备');
        }
    }

    function reqImageDownload(imageid, callback) {
        var type = getOsType();
        if (type == 'Android') {
            Android.imageDownload(imageid, callback);
        } else if (type == 'IOS') {
            ios_imageDownload(imageid, callback);
        } else {
            $.toast('暂时不支持该设备');
        }
    }

    function toast(msg) {
        var type = getOsType();
        if (type == 'Android') {
            Android.showToast(msg);
        } else if (type == 'IOS') {
            $.toast(msg);
        } else {
            $.toast('暂时不支持该设备');
        }
    }

    $(document).on("click", ".add-user-to-group", function () {
        var reqUri = $(this).attr('url-data');
        var groupId = $(this).attr('group-id');
        var userIds = [];
        $('input[name=select_user]').each(function () {
            var checkedFlag = $(this).prop('checked');
            var siteUserId = $(this).attr('id');
            if (checkedFlag) {
                userIds.push(siteUserId);
            }
        });
        if (userIds.length < 1) {
            $.toptip('添加成员不能为空', 'error');
            return false;
        }
        $('#add_group_user').attr('site-user-id', JSON.stringify(userIds));
        $.confirm({
            title: '添加用户',
            text: '确定要添加选中用户到当前群？',
            onOK: function () {
                var params = {
                    "groupMembers": userIds,
                    "siteGroupId": groupId
                };
                reqUrl(reqUri, JSON.stringify(params), "addGroupUser");
                $.closeModal();
            },
            onCancel: function () {
                console.log('cancle');
                toast('取消操作');
                $.closeModal();
            }
        });
    });

    function addGroupUser(result) {
        if (result == 'success') {
            var siteUserIds = $('#add_group_user').attr('site-user-id');
            siteUserIds = JSON.parse(siteUserIds);
            $.each(siteUserIds, function (index, siteUserId) {
                console.log(index);
                $("[for=" + siteUserId + "]").remove();
            });
            $.toast('添加成功');
        } else {
            $.toast('添加失败', 'forbidden');
        }
    }

    var page = 0;
    var loading = false;


    function nonList() {
        if (loading) {
            $('.loadmore_site_user').addClass('weui-loadmore_line');
            $('.loadmore_site_user span').html('暂无数据');
            $('.loadmore_site_user i').remove();
            return
        }
        page = page + 1;
        var reqUri = $('#init_url').attr('url-data');
        var groupId = $('#groupID').attr('groupId');
        var params = {
            'page': page.toString(),
            'group_id': groupId
        };
        reqUrl(reqUri, JSON.stringify(params), 'addUserToList');
        loading = true;
    }

    $(document.body).infinite().on("infinite", function () {
        nonList();
    });

    function addUserToList(results) {
        results = eval("(" + results + ")");
        console.log(results);
        var data = results.nonGroupMemberData;
        var html = '';
        $.each(data, function (index, user) {
            var html = '<label class="weui-cell weui-check__label" for="' + user.siteUserId + '"> <div class="weui-cell__hd"> <input type="checkbox" class="weui-check" name="select_user" id="' + user.siteUserId + '" > <i class="weui-icon-checked"></i> </div><div name="user_image" class="weui-cell__hd user_image  ' + user.userPhoto + ' " user-image="' + user.userPhoto + '" ></div>  <div class="weui-cell__bd"> <p>' + user.userName + '</p> </div> </label>';
            $('#user-lists').append(html);
        });
        loading = results.loading;
        if (loading == true) {
            $(".weui-loadmore__tips").html("到底啦");
        } else {
            $(".weui-loadmore__tips").html("加载更多");
        }
    }
</script>
</body>

</html>